$(document).ready(function () {
    validateRule();

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    }

    function submit() {

        var companyName = $("input[name='companyName']").val();
        var companyApplyControId = $("input[name='companyApplyControId']").val();
        var companyOrgNum = $("input[name='companyOrgNum']").val();
        var companyLocationCode = $("input[name='companyLocationCode']").val();
        var idCardNum = $("input[name='idCardNum']").val();
        var companyAddress = $("input[name='companyAddress']").val();
        var bakAllNum = $("input[name='bakAllNum']").val();
        var applyOpinion = $("input[name='applyOpinion']").val();
        var controPeopleId = $("input[name='controPeopleId']").val();
        var aera = $("input[name='aera']").val();
        var connectPeople = $("input[name='connectPeople']").val();
        var companyPhone = $("input[name='companyPhone']").val();
        var workTime = $("input[name='workTime']").val();
        var workType = $("input[name='workType']").val();

        var sel1 = $("#id-w-city-1")[0];
        var sel2 = $("#id-w-city-2")[0];
        var sel3 = $("#id-w-city-3")[0];
        //准备返回的结果，默认只获取省数据
        var privence=sel1.options[sel1.selectedIndex].text
        var city = sel2.options[sel2.selectedIndex].text;
        var county = sel3.options[sel3.selectedIndex].text;
        var status = $("input[name='status']").is(':checked') == true ? 0 : 1;

        var params = {
            companyName:companyName,
            companyApplyControId:companyApplyControId,
            companyOrgNum:companyOrgNum,
            companyLocationCode:companyLocationCode,
            idCardNum:idCardNum,
            companyAddress:companyAddress,
            bakAllNum:bakAllNum,
            applyOpinion:applyOpinion,
            controPeopleId:controPeopleId,
            aera:aera,
            connectPeople:connectPeople,
            companyPhone:companyPhone,
            status:status,
            companyApplyTime:workTime,
            county:county,
            privence:privence,
            city:city
        }
        $.ajax({
            type: "post",
            url: ctx + "system/company/add",
            data: params,
            success: function (r) {
                if (r.code == 0) {
                    location.href = ctx + 'index';
                } else {
                    $.modal.closeLoading();
                    $('.imgcode').click();
                    $(".code").val("");
                    $.modal.msg(r.msg);
                }
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        jQuery.validator.addMethod("isIdCardNo", function(value, element) {
            return this.optional(element) || IdCardUtils.checkIdCardNum(value);
        }, "请正确输入您的身份证号码");
        jQuery.validator.addMethod("IsGovernmentNum", function(value, element) {
            return this.optional(element) || governmentCode.verify(value);
        }, "请正确输入正确的企业代码");

        $("#signupForm").validate({
            debug: true,

            rules: {
                companyName: {
                    required:true,
                    minlength: 4,
                    maxlength: 150
                },
                companyOrgNum:{
                    required:true,
                    IsGovernmentNum:true
                },
                idCardNum:{
                    required: true,
                    isIdCardNo:true
                },
                connectPeople: {
                    required: true,
                    minlength:2,
                    maxlength:5
                },
                companyPhone: {
                    required: true
                },
                companyAddress:{
                    required:true,
                    rangelength	:[4,120]
                },
                companyApplyControId:{
                    required:true,
                    rangelength	:[4,200]
                },
                companyLocationCode:{
                    required:true,
                    rangelength:[6,6]
                }

                // workTime: {
                //     required: true,
                //     date:true
                // },
                // workType: {
                //     required: true
                // }
            },
            messages: {
                companyName: {
                    required: icon + "请输入您的企业名称"
                },
                companyOrgNum: {
                    required: icon + "请输入您的组织机构代码",
                    IsGovernmentNum:icon +"请输入正确的机构组织代码"
                },
                idCardNum:{
                    requestId:icon+"请输入身份证号",
                    isIdCardNo:"请输入正确的身份证号"
                },
                companyAddress: {
                    required: icon + "请输入您的详细地址"
                },
                connectPeople: {
                    required: icon + "请输入您的姓名"
                },
                phone: {
                    required: icon + "请输入您的电话号码"
                },
                companyApplyControId:{
                    required:icon + "请输入您的电话号码"

                },
                companyLocationCode:{
                    required:"请输入正确的区域代码",
                    rangelength:"请输入6位行政区域代码"
                }
                // workTime: {
                //     required: icon + "请输入您的申请返回时间"
                // },
                // workType: {
                //     required: icon + "请输入您的返岗类别"
                // }
            },
            submitHandler: function (form) {

                // console.log('form', form);
                // console.log('form params', form.serialize());
                submit();
                return false
            },
        })
    }
    function setCode(result) {
        if(result.code3){
            $("#companyLocationCode").val(result.code3)
            return;
        }
        if(result.code2){
            $("#companyLocationCode").val(result.code2)
            return;
        }
        if(result.code1){
            $("#companyLocationCode").val(result.code1)
            return;
        }
        return;

    }
    function onWCityS1(){
        onWCitySelect1()
        onWCitySubmit(setCode)
    }
    function onWCityS2(){
        onWCitySelect2()
        onWCitySubmit(setCode)
    }
    function onWCityS3(){
        onWCitySubmit(setCode)
    }
    $("#id-w-city-1").change(onWCityS1);
    $("#id-w-city-2").change(onWCityS2);
    $("#id-w-city-3").change(onWCityS3);


});